package com.example.yoho.mvp.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.example.yoho.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * A simple {@link Fragment} subclass.
 */
public class SortFragment extends Fragment implements View.OnClickListener {


    private BrandFragment brandFragment;
    private CategoryFragment categoryFragment;
    private RadioButton raBrond;
    private RadioButton raCategory;
    private FrameLayout raFl;
    private View view;
    private ImageView imOpen;
    private SlidingMenu slidingMenu;

    public SortFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_sort, container, false);
        initView();
        return view;
    }

    private void initView() {
        imOpen = (ImageView) view.findViewById(R.id.im_open);
        raBrond = (RadioButton) view.findViewById(R.id.ra_brond);
        raCategory = (RadioButton) view.findViewById(R.id.ra_category);
        raFl = (FrameLayout) view.findViewById(R.id.ra_fl);
        raBrond.setOnClickListener(this);
        raCategory.setOnClickListener(this);
        brandFragment = new BrandFragment();
        categoryFragment = new CategoryFragment();
        getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.ra_fl, brandFragment)
                .add(R.id.ra_fl, categoryFragment)
                .show(brandFragment)
                .hide(categoryFragment)
                .commit();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.silde_item, null);
        slidingMenu = new SlidingMenu(getContext());
        slidingMenu.attachToActivity(getActivity(), SlidingMenu.SLIDING_CONTENT);//绑定到指定的Context
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        slidingMenu.setMenu(view);
        slidingMenu.setBehindOffset(250);
        imOpen.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ra_brond:
                getActivity().getSupportFragmentManager().beginTransaction()
                        .show(brandFragment)
                        .hide(categoryFragment)
                        .commit();
                break;
            case R.id.ra_category:
                getActivity().getSupportFragmentManager().beginTransaction()
                        .show(categoryFragment)
                        .hide(brandFragment)
                        .commit();
                break;
            case R.id.im_open:
                slidingMenu.setMode(SlidingMenu.LEFT);
                slidingMenu.showMenu();
                break;
        }
    }
}
